package com.yoga.manager.admin.impl;

import com.yoga.common.appexception.AppErrorCodeEnum;
import com.yoga.common.appexception.AppException;
import com.yoga.common.bo.CourseBO;
import com.yoga.common.page.PageBO;
import com.yoga.common.qto.CourseQTO;
import com.yoga.dao.dao.CourseDao;
import com.yoga.dao.domain.CourseDO;
import com.yoga.manager.admin.AdminCourseManager;
import com.yoga.manager.beanUtil.CourseBeanUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by hdx on 2020/9/28.
 */
@Component
public class AdminCourseManagerImpl implements AdminCourseManager {
    Logger logger = LoggerFactory.getLogger(AdminCourseManagerImpl.class);

    @Resource
    private CourseDao courseDao;

    @Override
    public CourseBO addData(CourseBO courseBO) throws AppException {
        if (courseBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (courseBO.getYogaShopId() == null) {
            throw new AppException(AppErrorCodeEnum.NOT_SHOP_CHORES);
        }
        if (StringUtils.isEmpty(courseBO.getCourseName())) {
            throw new AppException(AppErrorCodeEnum.COURSE_NAME_NULL);
        }
        if (courseBO.getCourseLevel() == null || courseBO.getCourseLevel() <= 0) {
            throw new AppException(AppErrorCodeEnum.COURSE_LEVEL_NULL);
        }

        CourseDO old = courseDao.findByName(courseBO.getCourseName());
        if (old != null) {
            throw new AppException(AppErrorCodeEnum.COURSE_NAME_EXIST);
        }
        CourseDO courseDO = CourseBeanUtil.courseBOToDO(courseBO);
        int num = courseDao.insert(courseDO);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.INSERT_DATA_NULL);
        }
        courseBO.setCourseId(courseDO.getId());

        return courseBO;
    }

    @Override
    public Boolean updateData(CourseBO courseBO) throws AppException {
        if (courseBO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (courseBO.getCourseId() == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }
        if (!StringUtils.isEmpty(courseBO.getCourseName())) {
            CourseDO old = courseDao.findByName(courseBO.getCourseName());
            if (old != null) {
                throw new AppException(AppErrorCodeEnum.COURSE_NAME_EXIST);
            }
        }
        int num = courseDao.updateById(CourseBeanUtil.courseBOToDO(courseBO));
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return Boolean.TRUE;
    }

    @Override
    public Boolean deleteData(Long courseId) throws AppException {
        if (courseId == null) {
            throw new AppException(AppErrorCodeEnum.ID_IS_NULL);
        }

        int num = courseDao.deleteById(courseId);
        if (num <= 0) {
            throw new AppException(AppErrorCodeEnum.UPDATE_DATA_NULL);
        }

        return Boolean.TRUE;
    }

    @Override
    public PageBO<CourseBO> findForPage(CourseQTO courseQTO) throws AppException {
        if (courseQTO == null) {
            throw new AppException(AppErrorCodeEnum.PARAMETER_NULL);
        }
        if (courseQTO.getYogaShopId() == null) {
            throw new AppException(AppErrorCodeEnum.NOT_SHOP_CHORES);
        }
        PageBO<CourseBO> pageBO = new PageBO<>();
        pageBO.setQuery(courseQTO);
        Integer count = courseDao.findForPageCount(courseQTO);
        if (count <= 0) {
            return pageBO;
        }
        List<CourseBO> courseBOs = CourseBeanUtil.doList2BOList(courseDao.findForPage(courseQTO));
        pageBO.setTotalCount(count);
        pageBO.setData(courseBOs);

        return pageBO;
    }
}
